package net.zabuchy.activities.action;
 
import java.util.HashSet;
import java.util.Set;
 
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
 
import net.zabuchy.activities.dao.ActivityDao;
import net.zabuchy.activities.entity.HistoryAttribute;
import net.zabuchy.activities.entity.History_event;
import net.zabuchy.helper.DaoHelper;
import net.zabuchy.helper.SessionHelper;
import net.zabuchy.issue.bo.IssueBo;
import net.zabuchy.issue.entity.Issue;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
 
public abstract class ActivityAction extends  ActionSupport  {
 
    private static final Logger LOGGER = Logger.getLogger(ActivityAction.class.getName());
 
    private ActivityDao activityDao = DaoHelper.getActivityDao();
    private SessionHelper sessionBean = DaoHelper.getSessionBean();
    private IssueBo issueBo = DaoHelper.getIssueBo();
    
    private int historyEventId;
	private String summary;
	private String description;
	private String type;
	private String executor;
	private Issue issue;
	private Set<HistoryAttribute> historyAttributes = new HashSet<HistoryAttribute>(0);
    
    
    public String saveActivityEvent(int issueId, Set<HistoryAttribute> attributes, String type) {
        LOGGER.debug("start");
    	if (activityDao == null) {
            return Action.ERROR;
        }
        History_event historyEvent = new History_event();
		historyEvent.setSummary(summary);
		historyEvent.setDescription(description);
		historyEvent.setType(type);
		historyEvent.setExecutor(sessionBean.getExecutor().getUsername());
		historyEvent.setIssue(issueBo.findById(issueId));
		
		historyEvent.setHistoryAttributes(attributes);
		//dodajemy historyEvent id do kazdego attrybutu
		for(HistoryAttribute attr : attributes) {
			attr.setHistoryEvent(historyEvent);
		}
		
        LOGGER.debug("ActivityAction.add historyEvent: " + historyEvent.getSummary());
        activityDao.add(historyEvent);
        return Action.SUCCESS;
    }
 
    public ActivityDao getActivityDao() {
		return activityDao;
	}

	public void setActivityDao(ActivityDao activityDao) {
		this.activityDao = activityDao;
	}

	public SessionHelper getSessionBean() {
		return sessionBean;
	}

	public void setSessionBean(SessionHelper sessionBean) {
		this.sessionBean = sessionBean;
	}

	public IssueBo getIssueBo() {
		return issueBo;
	}

	public void setIssueBo(IssueBo issueBo) {
		this.issueBo = issueBo;
	}

	public int getHistoryEventId() {
		return historyEventId;
	}

	public void setHistoryEventId(int historyEventId) {
		this.historyEventId = historyEventId;
	}

	public String getSummary() {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getExecutor() {
		return executor;
	}

	public void setExecutor(String executor) {
		this.executor = executor;
	}

	public Issue getIssue() {
		return issue;
	}

	public void setIssue(Issue issue) {
		this.issue = issue;
	}

	public Set<HistoryAttribute> getHistoryAttributes() {
		return historyAttributes;
	}

	public void setHistoryAttributes(Set<HistoryAttribute> historyAttributes) {
		this.historyAttributes = historyAttributes;
	}

}